class UserController < ApplicationController
  layout "layouts/admin"
  
  def register
    @user = User.new(params[:user])
    if request.post?
      category = Category.new
      category.name = "Default"
      blog = Blog.new
      blog.categories << category
      @user.blog = blog
      if @user.save
        flash[:notice] = "User #{@user.name} created"
        redirect_to :action => "login"
      end
    end
  end

  def login
    session[:user_id] = nil
    if request.post?
      user = User.authenticate(params[:name], params[:password])
      if user
        session[:user_id] = user.id
        uri = session[:original_uri]
        session[:original_uri] = nil
        redirect_to(uri || {:controller => "admin/entry", :action => "list"})
      else
        flash[:notice] = "Invalid user/password combination"
      end
    end
  end

  def logout
    session[:user_id] = nil
    flash[:notice] = "User Logged out"
    redirect_to :action => "login"
  end
end
